import { create } from "zustand";

export type FieldType = {
  inputA?: number;
  inputB?: number;
  special?: boolean;
};

interface ComputeSomethingResponse {
  result: number;
  serverSideCompute(values: FieldType): Promise<void>;
}

export const useComputeSomething = create<ComputeSomethingResponse>((set) => ({
  result: 0,
  serverSideCompute: async (values: FieldType) => {
    console.log("Success:", values);
    const content: ComputeSomethingResponse = await (
      await fetch(
        `http://localhost:5108/compute?a=${values.inputA}&b=${values.inputB}&special=${values.special}`
      )
    ).json();
    set({
      result: content.result,
    });
  },
}));
